package org.foj.dao.impl;

import org.appfuse.dao.hibernate.GenericDaoHibernate;
import org.foj.dao.IssueDao;
import org.foj.model.Issue;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Conjunction;

import java.util.List;

/**
 * @author jeremy anderson
 */
public class IssueDaoImpl extends GenericDaoHibernate<Issue, Long> implements
		IssueDao {

	public IssueDaoImpl() {
		super(Issue.class);
	}

	@SuppressWarnings("unchecked")
	public List<Issue> findByCriteria(String projectName, String issueType,
			String severity, String status) {
		Conjunction conjunction = Restrictions.conjunction();

		if (isNotNullOrEmpty(projectName)) {
			conjunction.add(Restrictions.eq("project", projectName));
		}

		if (isNotNullOrEmpty(issueType)) {
			conjunction.add(Restrictions.eq("type", issueType));
		}

		if (isNotNullOrEmpty(severity)) {
			conjunction.add(Restrictions.eq("severity", severity));
		}

		if (isNotNullOrEmpty(status)) {
			conjunction.add(Restrictions.eq("status", status));
		}

		return getSession().createCriteria(Issue.class).add(conjunction).list();
	}

	private boolean isNotNullOrEmpty(String value) {
		return value != null && !value.trim().equals("");
	}

}
